package com.bianyuanren.v4.service.dynamicreport.impl;

import java.util.Date;
import java.util.List;

import com.bianyuanren.common.utils.StringUtils;
import com.bianyuanren.framework.util.ShiroUtils;
import com.bianyuanren.v4.service.dynamicreport.IPpDynamicReportService;
import com.bianyuanren.v4.mapper.dynamicreport.PpDynamicReportMapper;
import com.bianyuanren.v4.mapper.files.PpFilesMapper;
import com.bianyuanren.v4.pojo.dynamicreport.PpDynamicReport;
import com.bianyuanren.v4.pojo.files.PpFiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bianyuanren.common.core.text.Convert;

/**
 * 举报动态Service业务层处理
 * 
 * @author bianyuanren
 * @date 2020-05-14
 */
@Service
public class PpDynamicReportServiceImpl implements IPpDynamicReportService
{
    @Autowired
    private PpDynamicReportMapper ppDynamicReportMapper;

    @Autowired
    private PpFilesMapper ppFilesMapper;

    /**
     * 查询举报动态
     * 
     * @param id 举报动态ID
     * @return 举报动态
     */
    @Override
    public PpDynamicReport selectPpDynamicReportById(Long id)
    {
        return ppDynamicReportMapper.selectPpDynamicReportById(id);
    }

    /**
     * 查询举报动态列表
     * 
     * @param ppDynamicReport 举报动态
     * @return 举报动态
     */
    @Override
    public List<PpDynamicReport> selectPpDynamicReportList(PpDynamicReport ppDynamicReport) {
        List<PpDynamicReport> ppDynamicReports = ppDynamicReportMapper.selectPCDynamicReportList(ppDynamicReport);
        //查询出图片
        ppDynamicReports.parallelStream().forEach(p -> {
            p.setPpFilesList(ppFilesMapper.selectPpFilesByFileIdAndFileSourceAll(p.getId(), 6));
        });
        return ppDynamicReports;
    }

    /**
     * 新增举报动态
     * 
     * @param ppDynamicReport 举报动态
     * @return 结果
     */
    @Override
    public int insertPpDynamicReport(PpDynamicReport ppDynamicReport)
    {
        Date date = new Date();
        ppDynamicReport.setDynamicCreateTime(date);
        ppDynamicReport.setUserId(ShiroUtils.getAppUserId());
        int result = ppDynamicReportMapper.insertPpDynamicReport(ppDynamicReport);
        if (StringUtils.isNotBlank(ppDynamicReport.getDynamicReportImage())) {
            String[] images = ppDynamicReport.getDynamicReportImage().split(",");
            for (int i = 0; i < images.length; i++) {
                PpFiles ppFiles = new PpFiles();
                ppFiles.setFileId(ppDynamicReport.getId());
                ppFiles.setFileSource(6);
                ppFiles.setFileType(1);
                ppFiles.setCreateTime(date);
                ppFiles.setFileUrl(images[i]);
                //添加
                ppFilesMapper.insertPpFiles(ppFiles);
            }
        }
        return result;
    }

    /**
     * 修改举报动态
     * 
     * @param ppDynamicReport 举报动态
     * @return 结果
     */
    @Override
    public int updatePpDynamicReport(PpDynamicReport ppDynamicReport)
    {
        return ppDynamicReportMapper.updatePpDynamicReport(ppDynamicReport);
    }

    /**
     * 删除举报动态对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePpDynamicReportByIds(String ids)
    {
        return ppDynamicReportMapper.deletePpDynamicReportByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除举报动态信息
     * 
     * @param id 举报动态ID
     * @return 结果
     */
    @Override
    public int deletePpDynamicReportById(Long id)
    {
        return ppDynamicReportMapper.deletePpDynamicReportById(id);
    }
}
